package com.bosssoft.gateway.authorization.util;

import com.bosssoft.gateway.authorization.pojo.dto.LoginDTO;
import common.core.JwtPayloadHolder;
import common.exception.code.BizErrorCode;
import common.exception.parse.BusinessException;

import java.util.Map;

/**
 * @author 郭其鑫
 */
public class ParseTokenUtil {
    public static LoginDTO parseToken(String token) {
        Object object = JwtPayloadHolder.getPayload().get("token");
        if (object instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) object;
            LoginDTO loginDTO = new LoginDTO();
            // 可在此修改其它属性
            loginDTO.setId(Long.parseLong(map.get("id").toString()));
            loginDTO.setName((String) map.get("name"));
            loginDTO.setCode((String) map.get("code"));
            loginDTO.setTenantId(Long.parseLong(map.get("tenantId").toString()));
            loginDTO.setOrgId(Long.parseLong(map.get("orgId").toString()));
            loginDTO.setCompanyId(Long.parseLong(map.get("companyId").toString()));
            // 解析角色列表
            return loginDTO;
        } else {
            throw new BusinessException(BizErrorCode.UNDEFINED, "解析出了未定义的token");
        }


    }
}
